//
//  CYMoneySeg.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/7/6.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYMoneySeg.h"
#import "CYSelectBtn.h"
@interface CYMoneySeg(){
    CGFloat offset_width; //按钮相对于父视图左边的横向偏移量;
    block _seg_block;
}
@property (nonatomic, strong) CYLabel *titleLBL;
@end
@implementation CYMoneySeg
- (instancetype)initWithBlock:(block)block{
    self = [super init];
    if (self) {
        _seg_block = block;
        self.titleLBL.hidden = NO;
        [self setupBounds];
        [self setupMoneyBtn];
    }
    return self;
}

- (CYLabel *)titleLBL{
    if (!_titleLBL) {
        _titleLBL = [[CYLabel alloc]initWithText:@"充值金额" size:AL(PT(28)) textcolor:ColorHex(0x888888)];
        [self addSubview:_titleLBL];
        
        [_titleLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).with.offset(AL(PT(25)));
            make.top.equalTo(self).with.offset(AL(PT(30)));
            make.size.mas_equalTo(_titleLBL.bounds.size);
        }];
    }
    return _titleLBL;
}

- (void)setShowLine:(BOOL)showLine{
    if (_showLine != showLine){
        _showLine = showLine;
        if (showLine)
            [self setupLine];
    }
}

- (void)setTitle:(NSString *)title{
    if (_title != title) {
        _title = title;
        self.titleLBL.text = title;
        [_titleLBL mas_updateConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(_titleLBL.bounds.size);
        }];
    }
}
- (void)setupLine{
    for (int i = 0; i < 2; i ++) {
        CYView *line = [[CYView alloc]initWithColor:ColorHex(0xf5f5f9)];
        [self addSubview:line];
        
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, AL(PT(12))));
            if (i == 0)
                make.top.equalTo(self).with.offset(0);
            else
                make.bottom.equalTo(self).with.offset(0);
        }];
    }
}
- (void)setupMoneyBtn{
    
    NSArray *array = @[@[@"50", @"100", @"200"],@[@"300", @"500", @""]];
    CGFloat width = (SCREEN_WIDTH - AL(PT(25 *4))) / 3.0;
    for (int i = 0; i < 2; i ++) {
        for (int j = 0; j < 3; j ++) {
            WS(self)
            CYSelectBtn *button = [[CYSelectBtn alloc]initWithTitle:array[i][j] size:AL(PT(26)) selectImage:@"gouxuan" block:^(CYButton *button) {
                [weakSelf performSelector:@selector(actionForButton:) withObject:button];
            }];
            [button addTarget:self action:@selector(actionForButton:) forControlEvents:UIControlEventTouchUpInside];
            button.tag = (i == 0)?(200 + j):(203 + j);
            button.showTextfield = (i == 1 && j == 2)?YES:NO; //设置最后一个  显示按钮的字控件textfield
            [self addSubview:button];
            
            [button mas_makeConstraints:^(MASConstraintMaker *make) {
                make.size.mas_equalTo(CGSizeMake(width, AL(PT(80))));
                make.left.equalTo(self).with.offset(AL(PT(25)) + (width + AL(PT(25))) * j);
                make.top.equalTo(_titleLBL.mas_bottom).with.offset(AL(PT(25)) + (AL(PT(80)) + AL(PT(25))) * i);
            }];
        }
    }
}

//设置本身的bounds
- (void)setupBounds{
    
    offset_width = (SCREEN_WIDTH - AL(PT(200 *3))) / 4.0;
    CGFloat height = AL(PT(30 * 3 + 12 * 2 + AL(PT(25)) + 80 * 2 + _titleLBL.bounds.size.height));
    self.bounds = CGRectMake(0.0, 0.0, SCREEN_WIDTH, height);
    
}
/*按钮事件*/
- (void)actionForButton:(CYSelectBtn *)sender{
    sender.selected = YES;
    [self ergodicSubView:^(CYView *view) {
        CYSelectBtn *button = (CYSelectBtn *)view;
        if (button.tag != sender.tag && button.tag >= 200){
            button.selected = NO;
            [button endEditing:YES];
        }
    }];
    _seg_block(sender.money);
    
    //    button.selected = !button.selected;
    //    for (UIView *subview in [self subviews]) {
    //        if ([subview isKindOfClass:[CYSelectBtn class]] && subview.tag != button.tag ){ //筛选出未选中的button
    //            CYSelectBtn *no_select_button =  (CYSelectBtn *)subview;
    //            no_select_button.selected = NO;
    //            [no_select_button endEditing:YES];
    //        }
    //    }
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
